﻿CREATE PROCEDURE [dbo].[proc_Finance_RunningAccount_UpdateOrderRecord]
	(
		@Id int,
		@OrderId nvarchar(50),
		@PayId int
	)
AS
begin
	--插入数据
	Insert Into Order_PaymentRecord
	(
		RId
		,OId
		,CusId
		,CusName
		,SalesmanAccount
		,Salesman
		,CreateDate
		,Prices
		,PaymentTypeId
		,PaymentType
		,BankId
		,Bank
		,SWId
		,SWName
		,FinanceFlag
		,FinanceDate
		,CompanyId
		,Description
	)
	Select
		(Select RId From Finance_RunningAccountCopy Where Id=@Id)
		,OId
		,CusId
		,CusName
		,SalesmanAccount
		,Salesman
		,CreateDate
		,(Select Income From Finance_RunningAccountCopy Where Id=@Id)
		,PaymentTypeId
		,PaymentType
		,BankId
		,Bank
		,SWId
		,SWName
		,1
		,GetDate()
		,CompanyId
		,Description
	From Order_PaymentRecord  Where Id=@PayId
	
	UPDATE [Finance_RunningAccount] SET Renling = 2 WHERE id =(SELECT top 1 RId from Finance_RunningAccountCopy WHERE id=@Id)
	--删除中间数据
	Delete From Finance_RunningAccountCopy Where Id=@Id	
end
	RETURN
